package com.zjs.practice.controller;

import com.zjs.practice.vo.MessageParam;
import com.zjs.practice.domain.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 测试控制器1
 * @Author zhu
 * @Date 2019/12/31 21:37
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/test1")
@Api(value = "测试控制器1")
public class TestController1 {
    @ApiOperation(value="测试")
    @RequestMapping(value ="/test", method= RequestMethod.POST)
    public Message test(HttpServletResponse response, MessageParam message){
        log.info("测试入参{}", message);
        if (true || message.getId() == 1) {
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("商户交易数据");
            HSSFRow row = null;
            //创建第一个单元格
            row = sheet.createRow(0);
            row.setHeight((short) (27 * 20));
            row.createCell(0).setCellValue("签到信息"); //为第一行单元格设值
            //合并单元格  CellRangeAddress(起始行号，终止行号， 起始列号，终止列号）
            CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 2);
            sheet.addMergedRegion(rowRegion);
            //设置表头
            row = sheet.createRow(1);
            //设置行高
            row.setHeight((short) (23 * 20));
            //在第一行设置列名
            row.createCell(0).setCellValue("商户编号");
            row.createCell(1).setCellValue("签购单名称");
            row.createCell(2).setCellValue("支付渠道");

//        for (int i = 0; i < respVos.size(); i++) {
//            row = sheet.createRow(i + 2);
//            TradeRespVo tradedata = respVos.get(i);
//            row.createCell(0).setCellValue(tradedata.getMno());
//            row.createCell(1).setCellValue(tradedata.getShopName());
//            row.createCell(2).setCellValue(tradedata.getPayType());
//
////            CellStyle cellStyle = wb.createCellStyle();
////            HSSFCell cell = row.createCell(2);
////            HSSFDataFormat format = wb.createDataFormat();
////            cellStyle.setDataFormat(format.getFormat("yyyy-MM-dd HH:mm:ss"));
////            cell.setCellValue(record.getRecordSignInTime());
////            cell.setCellStyle(cellStyle);
//        }
            sheet.setDefaultRowHeight((short) (17 * 20));
            //列宽自适应
            for (int i = 0; i <= 2; i++) {
                sheet.setColumnWidth(i, 20 * 256);
            }
            //返回数据
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            OutputStream os = null;
            try {
                os = response.getOutputStream();
                //默认Excel名称
                response.setHeader("Content-disposition", "attachment;filename=MerchantTradeInfo.xls");
                wb.write(os);
                os.flush();
            }catch (IOException e) {
                log.error("导出流异常{}", e);
            } finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        log.error("导出流关闭异常{}", e);
                    }
                }
            }
//        // 指定允许其他域名访问
//        response.addHeader("Access-Control-Allow-Origin","*");
//        // 响应类型
//        response.addHeader("Access-Control-Allow-Methods","POST");
//        // 响应头设置
//        response.addHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
//        //响应内容支持中文
//        response.setContentType("text/html;charset=utf-8");
            return Message.ok();
        } else {
            return Message.error();
        }

    }
    @CrossOrigin(origins = "http://127.0.0.1:8020")
    @RequestMapping(value ="/test2", method= RequestMethod.POST)
    public String test2(){
        return "测试内容";
    }
}
